package com.culturalCenter.dataCenter.service.impl.site;

import com.culturalCenter.dataCenter.Utils.DateFormatUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.site.UsedRateResponse;
import com.culturalCenter.dataCenter.customEntity.site.CustomSiteUsedCount;
import com.culturalCenter.dataCenter.entity.site.SiteBookingMonth;
import com.culturalCenter.dataCenter.mapper.site.SiteBookingMonthMapper;
import com.culturalCenter.dataCenter.mapper.site.SiteMapper;
import com.culturalCenter.dataCenter.service.site.SiteBookingMonthService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/****
 * @Author:
 * @Description:SiteBookingMonth业务层接口实现类
 * @Date
 *****/
@Service
public class SiteBookingMonthServiceImpl implements SiteBookingMonthService {

    @Resource
    private SiteBookingMonthMapper siteBookingMonthMapper;
    @Resource
    private SiteMapper siteMapper;

    @Override
    public PageInfo<SiteBookingMonth> findPage(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> siteBookingMonthMapper.findPageAll());
    }

    @Override
    public Integer delete(String id) {
        return siteBookingMonthMapper.deleteById(id);
    }

    @Override
    public Integer update(SiteBookingMonth siteBookingMonth) {
        return siteBookingMonthMapper.update(siteBookingMonth);
    }

    @Override
    public Integer add(SiteBookingMonth siteBookingMonth) {
        return siteBookingMonthMapper.insert(siteBookingMonth);
    }

    @Override
    public SiteBookingMonth findById(String id) {
        return siteBookingMonthMapper.queryById(id);
    }

    @Override
    public List<SiteBookingMonth> findAll(SiteBookingMonth siteBookingMonth) {
        List<SiteBookingMonth> siteBookingMonths = new ArrayList<>();
        List<?> list = siteBookingMonthMapper.queryAll(siteBookingMonth);
        list.forEach(element -> siteBookingMonths.add((SiteBookingMonth) element));
        return siteBookingMonths;
    }

    @Override
    @Deprecated
    public UsedRateResponse getUsageRate(Date date) {
        String dateStr = DateFormatUtil.formatMonth(date);
        UsedRateResponse usedRateResponse = new UsedRateResponse();
        Map<String, List<CustomSiteUsedCount>> siteMap = new HashMap<>();
        List<String> siteArea = siteMapper.getSiteArea();
        if (dateStr.equals(DateFormatUtil.format(new Date()))) {
            siteArea.forEach(area -> siteMap.put(area, siteBookingMonthMapper.queryByDate(area, date)));
            usedRateResponse.setHotSiteList(siteBookingMonthMapper.queryHostSiteListByDate(date));
            usedRateResponse.setUnusedSiteList(siteBookingMonthMapper.queryUnusedSiteListByDate(date));
        } else {
            siteArea.forEach(area -> siteMap.put(area, siteBookingMonthMapper.queryByMonth(area, date)));
            usedRateResponse.setHotSiteList(siteBookingMonthMapper.queryHostSiteListByMonth(date));
        }
        return usedRateResponse;
    }

}
